package it.valeandre.gollometro.c2dm;

import it.valeandre.gollometro.utils.Utils;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.RemoteViews;

public class C2DMReceiver extends BroadcastReceiver {
	public static final String PREFS_C2DM = "it.valeandre.gollometro.c2dm";
	public static final String PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID = "it.valeandre.gollometro.c2dm.LAST_ID";
	public static final String PREFS_KEY_C2DM_OLD_REGISTRATION_ID = "it.valeandre.gollometro.c2dm.OLD_ID";
	protected static final String ACTION_REGISTRATION = "com.google.android.c2dm.intent.REGISTRATION";
	protected static final String ACTION_RECEIVE = "com.google.android.c2dm.intent.RECEIVE";
	protected static final String EXTRA_REGISTRATION_ID = "registration_id";
	protected static final String EXTRA_ERROR = "error";
	protected static final String EXTRA_UNREGISTERED = "unregistered";
	protected static final String EXTRA_C2DM_PAYLOAD = "message";

	public void onReceive(final Context context, final Intent intent) { 
		Log.d("TAG", "onReceive");
		if (intent.getAction().equals(ACTION_REGISTRATION)) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals(ACTION_RECEIVE)) {
			showNotification(context, intent.getStringExtra(EXTRA_C2DM_PAYLOAD));
		}
	}

	private void handleRegistration(Context context, Intent intent) {
		String accountName = intent.getExtras().toString();
		Log.d("TAG", "intent: " + accountName);
		if (intent.getStringExtra(EXTRA_REGISTRATION_ID) != null) {
			String registration_id = intent.getStringExtra(EXTRA_REGISTRATION_ID);
			Log.d("TAG", "token: " + registration_id);
//			saveRegistrationId(context, registration_id);

		} else if (intent.getStringExtra(EXTRA_ERROR) != null) {
			Log.d("TAG", "token: EXTRA_ERROR");
		} else if (intent.getStringExtra(EXTRA_UNREGISTERED) != null) {
//			Log.d("TAG", "token: removing registration");
//			removeRegistrationId(context);
//			sendBroadcastUregistered(context);
		} else {
			
		}
		
//		String registration = intent.getStringExtra("registration_id"); 
//	    if (intent.getStringExtra("error") != null) {
//	    	Log.d("TAG", "error ");
//	    } else if (intent.getStringExtra("unregistered") != null) {
//	    	Log.d("TAG", "unregistered ");
//	    } else if (registration != null) {
//	    	Log.d("TAG", "registration ");
//	    }
	}

	/*
	 * Registration ID is saved on SharedPreferences. Any time the application
	 * needs to register for a push event, it uses the last known registration ID
	 * saved on preferences. Since the Registration ID may change, the Esselunga app
	 * will have to notify the server that such ID has changed. Therefore, the app
	 * must save "current registration ID" and "previous ID", so that it can 
	 * provide both to the backend ("substitute ID1 with ID2").
	 */
	private void saveRegistrationId(Context context, String registrationId) { 
		
		Utils.hardLong("C2DM Gollometro ID: " + registrationId);
		
		SharedPreferences prefs = context.getSharedPreferences(PREFS_C2DM, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		
		String currentRegistrationId = prefs.getString(PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID, null);
		
		if (currentRegistrationId != null) {
			editor.putString(PREFS_KEY_C2DM_OLD_REGISTRATION_ID, currentRegistrationId);
		} else {
			editor.remove(PREFS_KEY_C2DM_OLD_REGISTRATION_ID);
		}
		
		editor.putString(PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID, registrationId);
		editor.commit();
		
		C2DMUtils.checkRegistrationID(context);
	}
	
	/*
	 * This method will be changed once the "Registration ID update process" will
	 * be defined.
	 */
	protected void removeRegistrationId(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_C2DM, Context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.remove(PREFS_KEY_C2DM_LAST_KNOWN_REGISTRATION_ID);
		editor.remove(PREFS_KEY_C2DM_OLD_REGISTRATION_ID);
		editor.commit();
	}
	
	protected void showNotification(Context context, String message) {
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

		Notification notification = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis());

		RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
		contentView.setImageViewResource(R.id.notification_image, R.drawable.ic_launcher);
		contentView.setTextViewText(R.id.notification_title, "Informazioni da Gollometro");
		contentView.setTextViewText(R.id.notification_text, message);
		notification.contentView = contentView;

		Intent notificationIntent = new Intent();
		notificationIntent.setClassName("it.valeandre.gollometro.c2dm", "GollometroActivity");
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
		notification.contentIntent = contentIntent;

		notification.defaults = Notification.FLAG_AUTO_CANCEL;

		mNotificationManager.notify(message, 1, notification);
	}
}
